package com.hyperboost.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyperboost.dto.OpportunityPageQueryDTO;
import com.hyperboost.mapper.OpportunityMapper;
import com.hyperboost.result.PageResult;
import com.hyperboost.service.OpportunityService;
import com.hyperboost.vo.OpportunityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OpportunityServiceImpl implements OpportunityService {

    @Autowired
    private OpportunityMapper opportunityMapper;

    /**
     * 分页条件获取所有机会
     *
     * @param opportunityPageQueryDto
     * @return 分页结果
     */
    @Override
    public PageResult<OpportunityVO> pageQuery(OpportunityPageQueryDTO opportunityPageQueryDto) {
        PageHelper.startPage(opportunityPageQueryDto.getPageNum(), opportunityPageQueryDto.getPageSize());
        List<OpportunityVO> opportunityVOList = opportunityMapper.pageQuery(opportunityPageQueryDto);
        Page<OpportunityVO> page = (Page<OpportunityVO>) opportunityVOList;
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 获取机会详情
     *
     * @param opportunityId
     * @return
     */
    @Override
    public OpportunityVO getOpportunityDetail(Long opportunityId) {
        // 查询机会详情
        OpportunityVO opportunityVo = opportunityMapper.selectById(opportunityId);
        if (opportunityVo != null){
            // 机会详情查询成功，返回浏览量+1
            opportunityMapper.increaseViewCount(opportunityId);
        }
        return opportunityVo;
    }
}
